package com.huanying.goodjob.publishactivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.huanying.framework.BaseController;
import com.huanying.framework.PageBean;

@Controller
public class PublishactivityController extends BaseController{

	@Autowired
	private PublishactivityService activityService;
	private static Logger logger = Logger.getLogger(PublishactivityController.class);
	@RequestMapping("/show_add_publishactivity.do")
	public String show_add_publishactivity(String searchtitle,String searchcontext,String day,String page_num,Model model){
		
		Map<String,String> map=new HashMap<String,String>();
		PageBean pageBean = null;
		map.put("title",searchtitle);
		map.put("context",searchcontext);
		map.put("day",day);
		if (page_num == null) {
			page_num = "1";
		}
		try {
			pageBean = activityService.search_activity(map, super.getPageSize(), Integer.valueOf(page_num));
		} catch (NumberFormatException e) {
			logger.error("查询消息失败", e);
			// TODO Auto-generated catch block
		} catch (Exception e) {
			logger.error("查询消息失败", e);
			// TODO Auto-generated catch block
		}
		model.addAttribute("title",searchtitle);
		model.addAttribute("context",searchcontext);
		model.addAttribute("day",day);
		model.addAttribute("activitys", pageBean.getList());
		model.addAttribute("page", pageBean);

		return "/publishactivity/add_publishActivity";
	}
	@RequestMapping("/add_activity.do")
	@ResponseBody
	public Map<String,String> add_activity(@RequestBody Publishactivity activity,String context,String status,String title){
		Map<String,String> map=new HashMap<String,String>();
		activity.setContext(context);
		activity.setTitle(title);
		try {
			if(activity.getId()==0)
			{
			activity.setStatus(1);
			activityService.add_activity(activity);
			}
			else{
				activity.setStatus(Integer.parseInt(status));
				activityService.update_activity(activity);
			}
			map.put("title", title);
			map.put("context", context);
			map.put("flag","1");
			map.put("id",Integer.toString(activity.getId()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			map.put("flag","0");
			logger.error("发布活动失败",e);
		}
		return map;
	}
	
	@RequestMapping(value = "/get_activity.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody
	public Map<String, Object> get_jobtype(int id){
		Map<String, Object> map = new HashMap<String, Object>();
		Publishactivity activity;
		try {
			activity= activityService.get_activity(id);
			map.put("activity",activity);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("获取活动失败", e);
		}
		
		map.put("flag","1");
		return map;
	}
	@RequestMapping(value = "/delete_activity.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody
	public Map<String, Object> delete_activity(int id){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			activityService.delete_activity(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("删除活动失败", e);
		};
		map.put("flag","1");
		return map;
	}
	
	//前台获取活动消息
	@RequestMapping(value = "/front_get_activitys.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String front_get_activitys(String jsoncallback) {
		Map<String, Object> map = new HashMap<String, Object>();
		List list=new ArrayList();
		try {
			list=activityService.front_get_activitys();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("前台获取活动失败", e);
		}
		map.put("list",list);
		String str="";
		Gson gson=new Gson();
		str=gson.toJson(map);
		
		return jsoncallback+"("+str+")";
	}
	//前台获得具体某个活动
	@RequestMapping(value = "/front_get_activitybyId.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String front_get_activitysbyId(String jsoncallback,int id) {
		Map<String, Object> map = new HashMap<String, Object>();
		Publishactivity activity;
		try {
			activity=activityService.get_activity(id);
			map.put("activity",activity);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("前台获取单个活动失败",e);
		}
		String str="";
		Gson gson=new Gson();
		str=gson.toJson(map);
		return jsoncallback+"("+str+")";
	}
}
